<template>
  <el-dialog
    :title="'影像材料备案'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="150px">

    <el-form-item label="登记中心" prop="djzx"     >
      <el-select v-model="dataForm.djzx" placeholder="登记中心" style="width:100%">
          <el-option
            v-for="item in dataForm.bankList"
            :key="item.bankid"
            :label="item.bankname"
            :value="item.bankid">
          </el-option>
      </el-select>
    </el-form-item>

    <el-form-item label="地市" prop="seRegion" v-if="false">
      <el-select  @change="selectChanged" v-model="dataForm.seRegion" placeholder="区域" style="width:100%">
          <el-option
            v-for="item in dataForm.seRegionList"
            :key="item.code"
            :label="item.name"
            :value="item.code">
          </el-option>
      </el-select>
    </el-form-item>

     <el-form-item label="区县" prop="region" v-if="false">
      <el-select v-model="dataForm.region" placeholder="区县" style="width:100%">
          <el-option
            v-for="item in dataForm.regionList"
            :key="item.code"
            :label="item.name"
            :value="item.code">
          </el-option>
      </el-select>
    </el-form-item>


     <el-form-item label="备注说明" prop="comment">
      <el-radio-group v-model="dataForm.comment"  >
          <el-radio label="1">影像备案</el-radio>
          <el-radio label="0">影像材料备案注销</el-radio>
        </el-radio-group>
    </el-form-item>
    



    <el-form-item label="有效开始时间" prop="enabledate" v-if="dataForm.comment==1"  :class="{ 'is-required': dataForm.comment == '1' }" >
        <el-date-picker v-model="dataForm.enabledate" placeholder="开始日期"  format="yyyyMMdd" value-format="yyyyMMdd" style="width:100%" ></el-date-picker> 
    </el-form-item>
    <el-form-item label="有效结束时间" prop="disabledate" v-if="dataForm.comment==1" :class="{ 'is-required': dataForm.comment == '1' }" >
        <el-date-picker v-model="dataForm.disabledate" placeholder="开始日期"  format="yyyyMMdd" value-format="yyyyMMdd" style="width:100%" ></el-date-picker>      
    </el-form-item>

    <el-form-item label="上传影像">
      <el-upload :action="url" 
                           multiple     
                        ref="upload"             
                        :on-success="successHandle"
                        :file-list="fileList"
                        :show-file-list="true"
                        :on-exceed="handleExceed"
                        :data="{yxbabsm:this.yxbabsm, uid:this.uid}"
                        :before-remove="beforeRemove"
                        :on-remove="removeFile"
                        :before-upload="beforeUploadHandle"
                      
                                            >
                                              <el-button size="small" type="primary">点击上传</el-button>
                                            </el-upload>
    </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {

      var validateDisabledate = (rule, value, callback) => {
        if (this.dataForm.comment == '1' && !/\S/.test(value)) {
          callback(new Error('有效结束时间不能为空'))
        } else {
          callback()
        }
      }

      var validateEnabledate = (rule, value, callback) => {
        if (this.dataForm.comment == '1' && !/\S/.test(value)) {
          callback(new Error('有效开始时间不能为空'))
        } else {
          callback()
        }
      }

      return {
        visible: false,
        yxbabsm: '',
        uid: '',
        url: '',
        fileList: [],

        dataForm: {
          seqno: 0,
          region: '',
          seRegion: '',
          comment: '',
          enabledate: '',
          disabledate: '',
          regionList: [],
          seRegionList: [],
          bankList: [],
          djzx: ''
        },
        dataRule: {
          // seRegion: [
          //   { required: true, message: '地市不能为空', trigger: 'blur' }
          // ],
          // region: [
          //   { required: true, message: '区县不能为空', trigger: 'blur' }
          // ],
          djzx: [
            { required: true, message: '登记中心不能为空', trigger: 'blur' }
          ],
          comment: [
            { required: true, message: '备注说明不能为空', trigger: 'blur' }
          ],
          enabledate: [
            { validator: validateEnabledate, trigger: 'blur' }
          ],
          disabledate: [
             { validator: validateDisabledate, trigger: 'blur' }
          ]


        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.seqno = id || 0
        this.visible = true
        this.url = this.$http.adornUrl(`/bdc/bdcrecordimage/upload?token=${this.$cookie.get('token')}`)
        this.fileList = [];
        this.$http({
          url: this.$http.adornUrl(`/bdc/bdcrecordimage/item/list`),
              method: 'get',
              params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.seRegionList = data.regionList
              this.dataForm.bankList = data.bankList
            }
        })

        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
                   
          if (this.dataForm.seqno) {
            this.$http({
              url: this.$http.adornUrl(`/bdc/bdcrecordimage/info/${this.dataForm.seqno}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {

                this.dataForm.region = data.bdcrecordimage.region
                this.dataForm.seRegion = data.bdcrecordimage.seRegion

 
   
                this.dataForm.enabledate = data.bdcrecordimage.enabledate
                this.dataForm.disabledate = data.bdcrecordimage.disabledate
                this.dataForm.proxyno = data.bdcrecordimage.proxyno
                this.dataForm.seRegion = data.seRegion
                if (data.filename){
                   this.fileList.push({'name': data.filename})
                }            

              }
            })
          }
        })
      },



      removeFile(file, fileList){
          this.$http({
          url: this.$http.adornUrl(`/bdc/bdcrecordimage/remove`),
              method: 'post',
              data: this.$http.adornData({
                'proxyno': this.dataForm.proxyno
              })
          }).then(({data}) => {
            if (data && data.code === 0) {
               this.$message({
                  message: '删除成功',
                  type: 'success',
                  duration: 1500
                })
 
            }else{
              this.fileList = fileList
            }
        })
      },

 selectChanged(value){
          this.dataForm.region = '';
          this.$http({
          url: this.$http.adornUrl(`/sys/bdcregion/listByLevle`),
              method: 'get',
              params: this.$http.adornParams({'upregion':value})
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.regionList = data.list
            }
        })
      },
      // 上传成功
      successHandle (response, file) {

        if (response && response.code === 0) {
            this.$message({
              message: '上传文件成功',
              type: 'success',
              duration: 1500
            });
            if (!this.yxbabsm){
              this.yxbabsm = response.yxbabsm
            }
            // this.fileList.push(file.name);
            this.fileList.push({'name': file.name})
            console.log(this.fileList.length)
        } else {
          this.$message.error(response.msg)
          this.fileList = [];
        }
      },

      // 上传之前
      beforeUploadHandle (file) {
        this.uid = file.uid
        return true;
      },

      handleExceed(files, fileList) {

      },
      beforeRemove(file, fileList) {

          return this.$confirm(`确定移除 ${ file.name }？`);
       
        
      },



      // 表单提交
      dataFormSubmit () {

        if (this.$refs.upload.uploadFiles.length == 0){
            this.$message.error("请上传文件")
            return;
        }

        var self = this;
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {

            this.$http({
              url: this.$http.adornUrl(`/bdc/bdcrecordimage/send`),
              method: 'post',
              data: this.$http.adornData({
                'djzx': this.dataForm.djzx,
                'region': this.dataForm.region,
                'seRegion': this.dataForm.seRegion,
                'comment': this.dataForm.comment,
                'yxbabsm': this.yxbabsm,
                'enabledate': this.dataForm.enabledate,
                'disabledate': this.dataForm.disabledate,

              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '影像材料备案成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
